package com.design.twords.adatper;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.design.twords.R;
import com.design.twords.entity.ItemWordMeanChoice;
import com.design.twords.utils.MyApplication;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  2:17
 */
public class MeanChoiceAdapter extends RecyclerView.Adapter<MeanChoiceAdapter.MyViewHolder> {

    public interface OnItemClickListener {

        void onItemClick(int position, ItemWordMeanChoice meanChoice);

    }

    // 判断是否是第一次点击
    public static boolean isFirstClick = true;

    private OnItemClickListener listener;
    private LayoutInflater myInflater;
    private List<ItemWordMeanChoice> itemMeanChoices;
    private Context context;
    private int viewId;


    private static final String TAG = "MeanChoiceAdapter";

    public MeanChoiceAdapter(List<ItemWordMeanChoice> itemMeanChoices, Context context, int viewId) {
        this.myInflater = LayoutInflater.from(context);
        this.itemMeanChoices = itemMeanChoices;
        this.context = context;
        this.viewId = viewId;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(myInflater.inflate(viewId, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ItemWordMeanChoice item = itemMeanChoices.get(position);
        holder.cardView.setCardBackgroundColor(Color.parseColor("#F2F3F8"));
        holder.means.setTextColor(Color.parseColor("#333944"));
        holder.result.setVisibility(View.GONE);
        switch (position){
            case 0:
                holder.select.setText("A ");
                break;
            case 1:
                holder.select.setText("B ");
                break;
            case 2:
                holder.select.setText("C ");
                break;
            case 3:
                holder.select.setText("D ");
                break;
        }
        holder.means.setText(item.getWordMean());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.cardView.setCardBackgroundColor(Color.parseColor("#AEEEEE"));
                holder.result.setVisibility(View.VISIBLE);
                if (item.isRight() == ItemWordMeanChoice.WRONG){
                    holder.means.setTextColor(Color.RED);
                    Glide.with(MyApplication.getContext()).load(R.drawable.icon_wrong).into(holder.result);
                }else if (item.isRight() == ItemWordMeanChoice.RIGHT){
                    holder.means.setTextColor(Color.BLUE);
                    Glide.with(MyApplication.getContext()).load(R.drawable.icon_select_blue).into(holder.result);
                }

                listener.onItemClick(position, item);
            }
        });


    }

    @Override
    public int getItemCount() {
        return itemMeanChoices.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView select, means;
        private ImageView result;
        private CardView cardView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            select = itemView.findViewById(R.id.meanchoice_select);
            means = itemView.findViewById(R.id.meanchoice_mean);
            result = itemView.findViewById(R.id.meanchoice_result);
            cardView = itemView.findViewById(R.id.menchoice_cardview);
        }
    }
}
